package controller;

import com.github.pagehelper.PageInfo;
import constant.Constant;
import entity.CourseType;
import exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CourseService;
import service.CourseTypeService;
import util.AjaxResult;
import vo.CourseVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Shao Liwei
 * @date 2021/1/8 11:04
 * description:
 */
@Controller
@RequestMapping("/ct")
public class CourseTypeController {
    @Autowired
    CourseTypeService courseTypeService;

    @Autowired
    CourseService courseService;
    /**
     * 课程类别管理
     *
     * @param request
     * @return
     */
    @RequestMapping("/findBackCourseType111.do")
    public AjaxResult FindCourseType(HttpServletRequest request) {
        PageInfo<CourseType> pageInfo = courseTypeService.findBackCourseType(request);
        AjaxResult result = AjaxResult.isOk("查询成功", pageInfo);
        return result;
    }

    //查询所有课程类别
    @RequestMapping("/findBackCourseType.do")
    @ResponseBody
    public AjaxResult findAllCourseType(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(true, "success", null);
        PageInfo<CourseType> info = courseTypeService.findAllCourseType(request);
        result.setObj(info);
        return result;
    }

    /**
     * 添加课程类别
     *
     * @return
     */
    @RequestMapping("/addBackType.do")
    @ResponseBody
    public AjaxResult addBackType(HttpServletRequest request) {
        int result = courseTypeService.addBackType(request);
        if (result == -1) {
            AjaxResult result1 = AjaxResult.isFail("当前类别名称已存在");
            return result1;
        }
        AjaxResult results = ((result > 0) ? AjaxResult.isOk("添加类别成功") : AjaxResult.isFail("添加类别失败"));
        return results;
    }

    /**
     * 课程类别管理
     *
     * @param request
     * @return
     */
    @RequestMapping("/toggleStatus.do")
    @ResponseBody
    public AjaxResult toggleStatus(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(true, null, null);
        String msg = courseTypeService.toggleStatus(request);
        result.setMsg(msg);
        return result;
    }

    /**
     * 课程类别管理 查看
     *
     * @param request
     * @return
     */
    @RequestMapping("/showBackName.do")
    @ResponseBody
    public AjaxResult showBackName(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(true, "success", null);
        String id = request.getParameter("id");
        CourseType courseType = courseTypeService.showBackName(id);
        result.setObj(courseType);
        return result;
    }
    /**
     * 课程类别管理 修改
     *
     * @param request
     * @return
     */
    @RequestMapping("/modifyCourseTypeName.do")
    @ResponseBody
    public AjaxResult modifyCourseTypeName(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(true, null, null);
        String id = request.getParameter("id");
        String typeName = request.getParameter("type_name");
        String msg= courseTypeService.modifyBackName(id,typeName);
        result.setMsg(msg);
        return result;
    }

    //==================================

    //    加载课程类型数据（三级）
    @RequestMapping("/findCourseType.do")
    @ResponseBody
    public Map<String, List> findCourseType() {
        System.out.println("加载课程类型数据（三级）======");
        Map<String,List> map = new HashMap<>();
        try {
//            封装课程类型
            List<CourseType> courseTypes = courseTypeService.findCourseType();
            map.put("courseTypes",courseTypes);

//            封装一级标题分类下，点击量前四的课程
            List<CourseVo> courses = courseService.findAllTop4(courseTypes);
            map.put("courses",courses);
            System.out.println(courses);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        System.out.println("加载完成======");
        return map;
    }


    //组合查询，返回二级标题的集合
    @RequestMapping("/findByParentId.do")
    @ResponseBody
    public List<CourseType> findByParentId(Integer parent_id, HttpSession session){
        System.out.println("parent_id="+parent_id);
        List<CourseType> secondTypes = new ArrayList<>();
        try {
            secondTypes = courseTypeService.findsecondType(parent_id);
        }catch (ServiceException e){
            e.printStackTrace();
        }

//        将对应的方向信息存入session备用
        Map<Integer,List<CourseType>> direction = new HashMap<>();
        direction.put(parent_id,secondTypes);
        session.setAttribute(Constant.SESSION_DIRECTION,direction);
        System.out.println("get="+session.getAttribute(Constant.SESSION_DIRECTION));
        return secondTypes;
    }

}
